//
//  FastTower.m
//  TowerDefence
//
//  Created by K3 on 6/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FastTower.h"
#import "PlayScreen.h"
#import "Enemy.h"
#import "iCoreGUIController.h"

@implementation FastTower

-(id)initFastTowerWith:(GameLayer *)_gameLayer {
    self = [super initTowerObjectWith:_gameLayer];
    [self loadFastTower];
    maxRadiusFireTarget = 150.0f;
    //    CGSize size=CGSizeMake(maxRadiusFireTarget, maxRadiusFireTarget);
    //range.scale=maxRadiusFireTarget*2/range.contentSize.width;
    //    NSLog(@"width %f",range.contentSize.width);
    buyPrice = FAST_TOWER_CONSTRUCTION_PRICE;
    countDown = 15;
    
    return self;
}

-(void)loadFastTower {    
    temp = [[CCSprite alloc]initWithSpriteFrameName:@"fastTower_beforeShoot_01.png"];
    menuItemSpriteFastTower = [CCMenuItemSprite itemFromNormalSprite:temp selectedSprite:nil target:self selector:@selector(showOption)];
    menuItemSpriteFastTower.scale = 0.7f;
    menuFastTower = [CCMenu menuWithItems:menuItemSpriteFastTower, nil];
    menuFastTower.position = ccp(0, 0);
    [spriteBody addChild:menuFastTower];
}

-(void)fireTarget{
    if(!handler.mainGUIController.isMuteSound) {
        [handler.mainGUIController playEffect:AUDIO_SILVER_LIGHT];
    }
    
    if([handler.listGameObject containsObject:targetLocked]) {
        bullet = [[LightBullet alloc]initLightBulletWith:handler tower:self];
        bullet.spriteBody.position = ccp(spriteBody.position.x + 5, spriteBody.position.y + 20);
        bullet.spriteBody.anchorPoint = ccp(0.5, 0);
        
        CGPoint targetPos;
        if(((Enemy *)targetLocked).inBattle) {
            targetPos = targetLocked.spriteBody.position;
        } else {
            targetPos = [(Enemy *)targetLocked getPositionAfterTime:bullet.velocity];
        }
        targetPos = ccp(targetPos.x, targetPos.y + targetLocked.spriteBody.contentSize.height / 2 - 5);
        
        if(!CGPointEqualToPoint(targetPos, targetLocked.spriteBody.position)) {
            [(PlayScreen *)handler fireEnemy:targetPos tower:self bullet:bullet];
        }
        
    }
}

-(void)shootTargetAnimation {       
    NSMutableArray *frameBeforeShootAnimationArray = [self loadAnimationWithNumberOfFrames:14 withFrameName:@"fastTower_beforeShoot"];    
    CCAnimation *beforeShootAnimation = [CCAnimation animationWithFrames:frameBeforeShootAnimationArray delay:0.05];
    
    NSMutableArray *frameAfterShootAnimation = [self loadAnimationWithNumberOfFrames:3 withFrameName:@"fastTower_afterShoot"];
    CCAnimation *afterShootAnimation = [CCAnimation animationWithFrames:frameAfterShootAnimation delay:0.05];
    
    CCCallFunc *fireEnemyCallback = [CCCallFunc actionWithTarget:self selector:@selector(fireTarget)];
    
    [temp runAction:[CCSequence actions:[CCAnimate actionWithAnimation:beforeShootAnimation restoreOriginalFrame:NO], fireEnemyCallback, [CCAnimate actionWithAnimation:afterShootAnimation restoreOriginalFrame:NO], nil]];
}

-(void)update:(ccTime)_timeUpdate {
    countDown--;
    if(countDown == 0) {
        [self findAndFireTarget];
        countDown = 15;
    }
}

-(void)dealloc {        
    [temp stopAllActions];
    [temp removeAllChildrenWithCleanup:YES];
    [temp removeFromParentAndCleanup:YES];
    [temp release];
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    [self removeFromParentAndCleanup:YES];
    
    [self removeAnimationInFrameCache:@"fastTower_beforeShoot"];
    [self removeAnimationInFrameCache:@"fastTower_afterShoot"];
    
    [super dealloc];
}

@end
